package com.newegg.datafeed.adserving.api;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.newegg.datafeed.adserving.api.annotation.AccessRequired;
import com.newegg.datafeed.adserving.domain.Channel;
import com.newegg.datafeed.adserving.model.UserType;
import com.newegg.datafeed.adserving.response.Response;
import com.newegg.datafeed.adserving.service.ChannelService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags="获取通道信息接口")
@RestController
@RequestMapping("/api/channel")
public class ChannelAPI extends BaseAPI{
	
	@Autowired
	ChannelService channelService;
	
	@ApiOperation(value="获取全部广告通道", consumes="application/json", produces="application/json")
    @RequestMapping(value="/all", method = RequestMethod.GET)
	@ResponseBody
    public Response<List<Channel>> all() throws Exception {
		return SUCCESS(channelService.all());
    }
	
	@ApiOperation(value="查询指定用户的通道列表", consumes="application/json", produces="application/json")
    @RequestMapping(value="/byUser/{user}", method = RequestMethod.GET)
	@ResponseBody
	@AccessRequired(role={UserType.ADMIN, UserType.COMPANY})
    public Response<List<String>> byUser(@PathVariable String user) throws Exception {
		return SUCCESS(channelService.getUserChannels(user));
    }
	
	@ApiOperation(value="保存广告通道", consumes="application/json", produces="application/json")
    @RequestMapping(value="/save", method = RequestMethod.POST)
	@ResponseBody
	@AccessRequired(role={UserType.ADMIN})
    public Response<Channel> save(@RequestBody Channel channel) throws Exception {
		return SUCCESS(channelService.save(channel));
    }
	
	@ApiOperation(value="保存广告通道", consumes="application/json", produces="application/json")
    @RequestMapping(value="/update", method = RequestMethod.POST)
	@ResponseBody
	@AccessRequired(role={UserType.ADMIN})
    public Response<Channel> update(@RequestBody Channel channel) throws Exception {
		return SUCCESS(channelService.update(channel));
    }
	
	@ApiOperation(value="删除广告通道", consumes="application/json", produces="application/json")
    @RequestMapping(value="/delete", method = RequestMethod.POST)
	@ResponseBody
	@AccessRequired(role={UserType.ADMIN})
    public Response<Void> delete(@RequestParam String id) throws Exception {
		channelService.delete(id);
		return SUCCESS();
    }
}
